#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
#include "config.h"
#include "debug.h"
#include "gpio.h"
#include "user_interface.h"
#include "mem.h"
#include "user_config.h"
#include "espconn.h"
#include "httpclient.h"


static ETSTimer test_timer;

static void ICACHE_FLASH_ATTR test_timer_cb()
{
	//http_get("http://sensors.xiong.tw/allsensors?temp=1&humi=2&hcho=3&co2=4&tvoc=5&pm1=6&pm25=7&pm10=8&mac=123456789767&aqi=10", "", http_callback_example);
	http_post("http://sensors.xiong.tw/allsensors","temp=1&humi=2&hcho=3&co2=4&tvoc=5&pm1=6&pm25=7&pm10=8&mac_address=123456789767&aqi=9","", http_callback_usr);
}

void ICACHE_FLASH_ATTR post_data_server(char * data)
{
	//http_get("http://sensors.xiong.tw/allsensors?temp=1&humi=2&hcho=3&co2=4&tvoc=5&pm1=6&pm25=7&pm10=8&mac=123456789767&aqi=10", "", http_callback_example);
	http_post("http://sensors.xiong.tw/allsensors",data,"", http_callback_usr);
}

void ICACHE_FLASH_ATTR http_start()
{
	os_timer_disarm(&test_timer);
	os_timer_setfn(&test_timer, test_timer_cb, NULL);
	os_timer_arm(&test_timer, 10000, 1);
}

void ICACHE_FLASH_ATTR http_callback_usr(char * response, int http_status, char * full_response)
{
	if (http_status != HTTP_STATUS_GENERIC_ERROR) {
//		os_printf("----------full_response=%s\n", full_response);
		os_printf("----------response=%s\n", response);
		#if	defined(_COMM_PRO_SEL_MQTT)
			mqtt_disconnect_from_server();
		#elif defined(_COMM_PRO_SEL_HTTP)
			module_net_state = 2;
		#endif
	}
	else
	{

//		os_printf("----------http_status=%d\n", http_status);
	}
}
